import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import { getToken } from '../utils/storage'
const UserInfo = () => import('@/components/UserInfo/User.vue')
const Goods = () => import('@/components/Goods/Goods.vue')
const Default = () => import('@/components/Default/Default.vue')
const GoodsClass = () => import('@/components/GoodsCLass/GoodsClass.vue')
const Order = () => import('@/components/Order/Order.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [{
      path: '/userInfo',
      component: UserInfo
    }, {
      path: '/goods',
      component: Goods
    }, {
      path: '/default',
      component: Default
    }, {
      path: '/goodsClass',
      component: GoodsClass
    },, {
      path: '/order',
      component: Order
    },
    ]
  },
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫 判断跳转的页面是否需要登录
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    return next()
  }
  else {
    if (!getToken()) { return next('/login') }

    else if (to.matched.length === 0) {  //如果未匹配到路由
      return next('/default')
    }

  }
  next()
})
export default router
